package routers

import (
	"alan-snippet/internal/controller"
	"alan-snippet/internal/middleware"

	"github.com/gin-gonic/gin"
)

func SetupRouter() *gin.Engine {
	r := gin.New()

	// 全局应用通用中间件
	// 顺序：Logger -> Recovery -> ErrorHandler -> CORS
	// TODO: 其中自定义错误处理仅仅在updatesnippet上试用了一下，毕竟太专业了，了解一下即可
	r.Use(middleware.Logger(), middleware.Recovery(), middleware.ErrorHandler(), middleware.CORSMiddleware())

	v1 := r.Group("/api/v1")
	{
		users := v1.Group("/users")
		{
			users.POST("/register", controller.RegisterUser)
			users.POST("/login", controller.LoginUser)
		}

		snippets := v1.Group("/snippets")
		snippets.Use(middleware.JWTAuth())
		{
			snippets.POST("/", controller.CreateSnippet)
			snippets.GET("/", controller.SnippetList)
			snippets.GET("/:id", controller.GetSnippetById)
			snippets.GET("/popular", controller.GetPopularSnippets)
			snippets.PUT("/:id", controller.UpdateSnippet)
			snippets.DELETE("/:id", controller.DeleteSnippet)

		}

		tags := v1.Group("/tags")
		tags.Use(middleware.JWTAuth())
		{
			tags.GET("", controller.ListTags)
		}
	}

	return r
}
